local japi = require 'jass.japi'
local japi2 = require 'util.japi2'


--- editbox ---
local editbox = {}
setmetatable(editbox,editbox)

---存放所有的按钮实例
editbox.alleditboxs = {}

local mt = extends(class.ui_base){}

--- 类型
mt.type = 'editbox'

--- 魔兽控件类型
mt._type = 'EDITBOX'

--- 识别名称
mt._name = '输入框'

--- 默认文字
mt.text = "editbox"

--- 字体
mt.font = "MasterFont"

--- 字体大小
mt.font_size = 12

--- 字体间距
mt.spacing = 0

--- 文本颜色
mt.color = 0xffffffff

--- 字数限制
mt.limit = 32

--- 创建控件
function mt.new(parent, x, y, w, h)
    return class.editbox:builder
    {
        parent = parent,
        w = w,
        h = h,
        x = x,
        y = y,
    }
end 

--- 初始化
function mt:init()
    if self.handle then return end
    class.ui_base.init(self) 
    local handle = japi.CreateFrameByTagName(self._type,self._name,self.parent.handle,self.type,self._index)
    if not handle or handle == 0 then 
        log.error('创建editbox失败')
        class.ui_base.remove(self)
        return  
    end
    
    self.handle = handle

    local childs = japi.FrameGetChildlist(handle)
    
    -- self.child1 = class.text.convert(childs[1])  -- 1 SCROLL 显示的文字面板 包含childs2里面的 5 个子级控件
    -- self.child2 = class.ui_base.convert(childs[2])       -- 2 TEXT 可能是未进入焦点时候的文本 就是灰色的文本
    -- self.child3 = class.ui_base.convert(childs[3])       -- 3 HIGHLIGHT 高光 候选文本的底板
    -- self.child4 = class.ui_base.convert(childs[4])       -- 4 TEXT 候选文字

    -- 卧槽 下面这玩意的父级居然是 GameUI 
    local child1  = class.ui_base.convert(childs[1], nil, class.ui_base.parent)          
    local childs2 = japi.FrameGetChildlist(child1.handle)

    child1.child1 = class.text.convert(childs2[1], self)   -- 输入后的文本
    child1.child2 = class.text.convert(childs2[2], self)   -- 光标文本 可以修改颜色 和 字体
    -- child1.child3 = class.ui_base.convert(childs2[3])     -- HIGHLIGHT 输入时文本的高光 没有用
    child1.child4 = class.text.convert(childs2[4], self)   -- 输入时的文本 
    -- child1.child5 = class.ui_base.convert(childs2[5])     -- HIGHLIGHT 选择时文本的高光 没有用

    self.child1 = child1
    self.child4 = class.text.convert(childs[4], self)

    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    self:set_font(self.font,self.font_size)
    self:set_color(self.color)
    self:set_spacing(self.spacing)
    self:set_limit(self.limit)
    self:set_text(self.text)
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    --- 注册监听器
    japi.RegisterFrameEvent(self.handle)
    editbox.alleditboxs[handle] = self
    class.ui_base.allframes[handle] = self
    return self
end 

---删除控件
function mt:remove()
    if self.removed then return end
    editbox.alleditboxs[self.handle] = nil
    self.child1:remove()
    -- self.child2:remove()
    -- self.child4:remove()
    class.ui_base.remove(self)
end

--- 设置输入框文字内容
--- @params name text
function mt:set_text(text)
    japi.FrameSetText(self.handle,text)
end 

--- 获取输入框文字内容
--- @return string
function mt:get_text()
    if self.removed then 
        return self.text
    end
    return japi.FrameGetText(self.handle)
end

--- 设置字数限制
function mt:set_limit(limit)
    self.limit = limit 
    if self.removed then return end 
    japi.DzFrameSetTextSizeLimit(self.handle,self.limit)
end 

--- 获取字数限制
function mt:get_limit()
    if self.removed then return self.limit end 
    return japi.DzFrameGetTextSizeLimit(self.handle)
end 

--- 设置文本字体和大小
function mt:set_font(font_path,font_size)
    self.child1.child1:set_font(font_path,font_size)
    self.child1.child2:set_font(font_path,font_size)
    self.child1.child4:set_font(font_path,font_size)
    self.child4:set_font(font_path,font_size)
end

--- 设置文本间距 
function mt:set_spacing(spacing)
    self.child1.child1:set_spacing(spacing)
    self.child1.child4:set_spacing(spacing)
end

--- 设置颜色
function mt:set_color(color)
    if self.removed then return end
    self.child1.child1:set_color(color)
    self.child1.child4:set_color(color)
end

--- 获取颜色
function mt:get_color()
    return self.child1.child1:get_color()
end

--- 设置焦点
function mt:set_focus(is_enable)
    if is_enable then 
        japi.FrameSetEditFocus(self.handle,true)
    else
        japi.FrameSetEditFocus(self.handle,false)
    end
end

--- 设置输入框是否可以激活输入法
function mt:set_lmme_enable(flag)
    if self.removed then return end
    japi2.SetEditBoxDisableImme(self.handle, not(flag))
end

--- 事件注册
editbox.events = {

    [9] = function(self) 
        self:notify('on_editbox_text_changed',self:get_text())
    end,


    --- 没办法 垃圾魔兽中文占 1 个字符我是没想到的 
    [-1] = function()
        if ac.ignore_flag ~= true then 
            local handle = japi.GetMouseFocus()
            local edit = editbox.alleditboxs[handle]
            if not edit then 
                editbox.focus = nil
                return false 
            end 
            if edit == editbox.focus then 
                return false 
            end

            local inputStr = edit:get_text()
            local lenInByte = #inputStr
            local width = 0
            local i = 1
            while (i <= lenInByte) do
                local curByte = string.byte(inputStr, i)
                local byteCount = 1
                if curByte > 0 and curByte <= 127 then
                    byteCount = 1 -- 1字节字符
                elseif curByte >= 192 and curByte < 223 then
                    byteCount = 2 -- 双字节字符
                elseif curByte >= 224 and curByte < 239 then
                    byteCount = 3 -- 汉字
                elseif curByte >= 240 and curByte <= 247 then
                    byteCount = 4 -- 4字节字符
                end
                i = i + byteCount -- 重置下一字节的索引
                width = width + 1 -- 字符的个数（长度）
            end
            if width <= 0 then 
                return false 
            end
            ac.ignore_flag = true
            edit:set_text("")
            ac.ignore_flag = false
            editbox.focus = edit
            return false
        end
    end,
}


-- ac.hardware:on "鼠标点击" (editbox.events[-1])
-- ac.hardware:on "鼠标右键点击" (editbox.events[-1])
class.editbox = mt

return mt
